<?php


namespace app\controller\api\v2_0;

use app\controller\api\Base;
use app\util\ReturnCode;
use app\util\Strs;
use app\util\Tools;
use app\validate\AdminStaff;
use app\model\AdminStaff as StaffModel;
use think\exception\ValidateException;
use think\facade\Request;
use think\facade\Validate;
use think\Response;

class Staff extends Base
{
    /**
     * @func v3/apply
     * @method POST
     * @desc 个人资料提交审核
     * @return Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Time: 2022/3/14 17:44
     */
    public function apply() : Response {
        try {
            validate(AdminStaff::class)->scene('v3Edit')
                ->append('phone|手机号', 'require')->check(input('post.'));
        } catch (ValidateException $e) {
            return $this->buildFailed(ReturnCode::PARAM_INVALID, $e->getError());
        }
        $postData = $this->request->post();
        $where['is_delete'] = 0;
        if (isset($postData['uuid'])) {
            // 重新审核携带
            $where['uuid'] = $postData['uuid'];
        } else {
            $where['phone'] = $postData['phone'];
        }
        $staff = (new StaffModel())->where($where)->find();
        if (!isset($postData['uuid']) && $staff) {
            return $this->buildFailed(ReturnCode::DATA_EXISTS, '手机号已被使用');
        }
        if (isset($postData['uuid']) && !$staff) {
            return $this->buildFailed(ReturnCode::RECORD_NOT_FOUND, '账户未找到');
        }
        if ($staff && $staff->status == StaffModel::AUDIT_PASS) {
            return $this->buildFailed(ReturnCode::UPDATE_FAILED, '账号已审核通过，请前往登录');
        }

        if (!$staff) {
            $staff = new StaffModel();
            $postData['uuid'] = substr(Strs::uuid(), 1, -1);
            $postData['password'] = Tools::userMd5(substr($postData['phone'], -6));
        }

        $postData['type'] = StaffModel::STUDENT;
        $postData['status'] = StaffModel::AUDIT_WAITING;


        if ($staff->save($postData) === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }
        return $this->buildSuccess();
    }

    /**
     * @func edit
     * @method POST
     * @desc 个人资料更新
     * @return Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Time: 2021/12/14 14:53
     */
    public function edit() : Response {
        try {
            validate(AdminStaff::class)->scene('v3Edit')
                ->append('uuid|用户标识', 'require')
                ->check(input('post.'));
        } catch (ValidateException $e) {
            return $this->buildFailed(ReturnCode::PARAM_INVALID, $e->getError());
        }
        $postData = Request::only(['avatar_url','name','sex','school_id','student_no','major','entrance_time','graduate_time','uuid']);
        $staff = (new StaffModel())->where('uuid', $postData['uuid'])
            ->where('is_delete', 0)
            ->where('status', StaffModel::AUDIT_PASS)->find();
        if (!$staff) {
            return $this->buildFailed(ReturnCode::RECORD_NOT_FOUND, '账户未找到');
        }
        if ($staff->save($postData) === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }
        return $this->buildSuccess();
    }
}